package com.yunhe.mybatis.web.user;

import com.yunhe.mybatis.entity.Deparment;
import com.yunhe.mybatis.entity.User;
import com.yunhe.mybatis.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/u/update")
public class UserUpdateServlet extends HttpServlet {
    private UserServiceImpl userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("UTF-8");

        String uname = req.getParameter("uname");
        String password = req.getParameter("passwor");
        Integer updateId=null;

        if (null != req.getParameter("updateId")){
            updateId = Integer .valueOf(req.getParameter("updateId"));
        }
        String name = req.getParameter("name");
        Integer age=null;
        if (null != req.getParameter("age")){
            age = Integer .valueOf(req.getParameter("age"));
        }

        Integer sex=null;
        if(null!=req.getParameter("sex")){
            sex = Integer.valueOf(req.getParameter("sex"));
        }

        String phone = req.getParameter("phone");
        String emall = req.getParameter("emall");

        Integer deparment1=null;
        if(null != req.getParameter("deparment")   &&   "" != req.getParameter("deparment")){
            deparment1 = Integer.valueOf(req.getParameter("deparment"));
        }
        String juese = req.getParameter("juese");
        User user = new User();
        Deparment deparment = new Deparment();

        user.setId(updateId);
        user.setUname(uname);
        user.setPassword(password);
        user.setName(name);
        user.setAge(age);
        user.setSex(sex);
        user.setEmall(emall);
        user.setPhone(phone);

        deparment.setId(deparment1);
        user.setRoule(juese);

        userService.update(user);
        req.getRequestDispatcher("/success.jsp").forward(req,resp);

    }
}
